// 导入request请求工具类
import {
    getBaseUrl,
    requestUtil,
    requestUpdateUtil
} from '../../utils/requestUtil.js';
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

    /**
     * 页面的初始数据
     */
    data: {
      baseUrl:'',
      ping:null,
      refid:null,
      show:false,
        tabs: [
            {
                id: 1,
                value: "待付款",
                isActive: false
            },
            {
                id: 2,
                value: "进行中",
                isActive: false
            },
            {
                id: 3,
                value: "待确认",
                isActive: false
            },
            {
              id: 4,
              value: "待评价",
              isActive: false
          },
          {
            id: 5,
            value: "已评价",
            isActive: false
        },
            
        ]
    },
    //接口参数
    QueryParams: {
        type: 0,
        page: 1, //第几页
        limit: 10 ,//每页记录数
        sort:'id',
        ispay:"未支付"
    },

    //总页数
    totalPage: 1,

    //根据标题索引激活选中的标签
    changeTitleByIndex(index) {
        let {
            tabs
        } = this.data;
        tabs.forEach((v, i) => i == index ? v.isActive = true : v.isActive = false);
        this.setData({
            tabs
        })
    },
    z_input(e){
        var text = e.detail.value;
        const index = e.currentTarget.dataset.bid;
        console.log("增加评价"+ JSON.stringify(e) )
        this.setData({ ping: text ,refid: index})  
    },
    //tab点击事件处理
    handleTabsItemChange(e) {
        // const {index}=e.currentTarget.dataset;
        const {
            index
        } = e.detail;
        //切换标题
        this.changeTitleByIndex(index);

        //获取订单列表
        this.QueryParams.type = index;
        this.QueryParams.page = 1;
        this.setData({
            orders: []
        })
        this.getOrders();
    },

    //获取订单
    async getOrders() {
        
        if(this.QueryParams.type == 0) {
             var res = await requestUtil({url: '/fuwuyuyue/page',data:this.QueryParams,method:"GET"});
        } else if(this.QueryParams.type != 0) {
            var isPay;
            if(this.QueryParams.type == 1) {
                isPay = '服务中'
            } else if(this.QueryParams.type == 2) {
                isPay = '已完成'
            }else if(this.QueryParams.type == 3) {
                isPay = '已确认'
            }else if(this.QueryParams.type == 4) {
                isPay = '已评价'
            }
            var res = await requestUtil({url: '/fuwujindu/page',data:{
                page: 1, //第几页
                limit: 10 ,//每页记录数
                sort:'id',
                fuwujindu: isPay
            },method:"GET"});
        }
        this.totalPage = res.data.total;
        if(this.QueryParams.page == 1) {
          this.setData({
            orders:[]
          })
        }
        console.log(this.data.orders,'this.data.ordersthis.data.ordersthis.data.orders')
        this.setData({
            orders: [...this.data.orders, ...res.data.list]
        })
    },


    UpdateStatusParams: {
        id: 154,
        status: 2
    },
    //修改订单
    async updateStatusOrders() {
        const res = await requestUpdateUtil({
            url: '/my/order/updateStatus',
            data: this.UpdateStatusParams
        });
        // this.totalPage = res.totalPage;
        // this.setData({
        //     orders: [...this.data.orders, ...res.orderList]
        // })
    },

    //确认收获
    async handlePay(e) {
        var index = e.target.dataset.index //点击列表选择的下标 请求用
        console.log("点击了确认收货   " + index)
        const res=await requestUtil({url:"/fuwuyuyue/update",method:"PUT",data:{id:index,ispay:'已支付'}});
      this.onShow();
    },
    //已确认
    async sales(e) {
        var index = e.target.dataset.index //点击列表选择的下标 请求用
        const res=await requestUtil({url:"/fuwujindu/update",method:"PUT",data:{id:index,fuwujindu:'已确认'}});
        this.onShow();
    },
    sale(){
      this.setData({
          show:'true'
      })
    },
    async addPing(){
        console.log(this.data.orders[this.data.refid],'snsns')
        const res=await requestUtil({url:"/pingjiaxinxi/add",method:"POST",data:{...this.data.orders[this.data.refid],pingjianeirong:this.data.ping,}});
        if(res.code == 0) {
            this.setData({
                show:'false'
            })
            wx.showToast({
                icon:'success',
              title: '评价成功',
              success:function(){
                  console.log('snddndn')
              }
            })
            const res=await requestUtil({url:"/fuwujindu/update",method:"PUT",data:{id:this.data.orders[this.data.refid].id,fuwujindu:'已评价'}});
        this.onShow();
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      const baseUrl = getBaseUrl();
      this.setData({
        baseUrl
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let pages = getCurrentPages();
        let currentPage = pages[pages.length - 1];
        const {
            type
        } = currentPage.options;
        this.changeTitleByIndex(type);
        this.QueryParams.type = type;
        this.QueryParams.page = 1;

        this.setData({
          orders:[]
        })
        console.log(this.data.orders,'this.data.orders')
        this.getOrders();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.QueryParams.page = 1;
        this.setData({
            orders: []
        })
        this.getOrders();
        //手动关闭等待效果
        wx.stopPullDownRefresh({

        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        console.log("触底了")
        if (this.QueryParams.page >= this.totalPage) {
            //没有下一页数据
            wx.showToast({
                icon:'none',
                title: '没有下一页数据了',
            })
        } else {
            this.QueryParams.page++;
            this.getOrders();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})
